package com.zh.modules.app.utils;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

public class SqlKeyword {
    private static final String SQL_REGEX = "'|%|--|insert|delete|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql";
    //左右模糊匹配
    private static final String LIKE = "_like";
    //左模糊匹配 %飞,例如名字中含飞结尾的
    private static final String LIKE_LEFT = "_likel";
    //右边模糊匹配 例如张%,姓名张开头
    private static final String LIKE_RIGHT = "_liker";
    //模糊匹配，不包含某个值
    private static final String NOT_LIKE = "_notlike";
    //不等于
    private static final String NOT_EQUAL = "_ne";
    //大于
    private static final String GT = "_gt";
    //小于
    private static final String LT = "_lt";
    //大于等于
    private static final String GE = "_ge";
    //小于等于
    private static final String LE = "_le";
    //为空
    private static final String IS_NULL = "_isnull";
    //不为空
    private static final String NOT_NULL = "_notnull";
    private static final String IGNORE = "_ignore";

    //统一多列模糊查询字段
    public static final String FUZZY = "fuzzy";

    public SqlKeyword() {
    }

    public static void buildCondition(Map<String, Object> query, QueryWrapper<?> qw) {
        if (!MapUtil.isEmpty(query)) {
            query.forEach((k, v) -> {
                if (ObjectUtil.isNotEmpty(v)) {

                    if(v instanceof CharSequence){
                        if(NumberUtil.isLong(v.toString())){
                            v = Long.valueOf(v.toString());
                        }
                    }

                    if (k.endsWith(NOT_EQUAL)) {
                        qw.ne(getColumn(k, NOT_EQUAL), v);
                    } else if (k.endsWith(NOT_LIKE)) {
                        qw.notLike(getColumn(k, NOT_LIKE), v);
                    } else if (k.endsWith(GT)) {
                        qw.gt(getColumn(k, GT), v);
                    } else if (k.endsWith(LT)) {
                        qw.lt(getColumn(k, LT), v);
                    } else if (k.endsWith(GE)) {
                        qw.ge(getColumn(k, GE), v);
                    } else if (k.endsWith(LE)) {
                        qw.le(getColumn(k, LE), v);
                    }else if (k.endsWith(IS_NULL)) {
                        qw.isNull(getColumn(k, IS_NULL));
                    } else if (k.endsWith(NOT_NULL)) {
                        qw.isNotNull(getColumn(k, NOT_NULL));
                    }else if (k.endsWith(LIKE)) {
                        qw.like(getColumn(k, LIKE), v);
                    }else if (k.endsWith(LIKE_LEFT)) {
                        qw.likeLeft(getColumn(k, LIKE_LEFT), v);
                    } else if (k.endsWith(LIKE_RIGHT)) {
                        qw.likeRight(getColumn(k, LIKE_RIGHT), v);
                    } else {
                        qw.eq(getColumn(k, ""), v);
                    }
                }
            });
        }
    }

    private static String getColumn(String column, String keyword) {
//        return StringUtil.humpToUnderline(StringUtil.removeSuffix(column, keyword));
        return StringUtil.removeSuffix(column, keyword);
    }


    public static String filter(String param) {
        return param == null ? null : param.replaceAll("(?i)'|%|--|insert|delete|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql", "");
    }
}
